<?php

/**
 * @Author: Mr.Mao
 * @Date:   2021-05-26 17:25:30
 * @Last Modified by:   Mr.Mao
 * @Last Modified time: 2021-05-27 17:57:04
 */
namespace app\weixin\model;
use app\BaseModel;
use think\facade\Cache;
use think\Db;
//*------------------------------------------------------ */
//-- 自定义组件订单模型
/*------------------------------------------------------ */
class WeixinVideoOrderModel extends BaseModel
{
	protected $table = 'weixin_video_order';
	public  $pk = 'id';
	protected $mkey = 'weixin_video_order_';

	/**
	 * [getOrderInfo 获取订单详情]
	 * @param  array  $param [请求参数]
	 */
	public function getOrderInfo($param)
	{
		$ACCESS_TOKEN = (new WeiXinMpModel)->getAccessToken();
        $url = "https://api.weixin.qq.com/shop/order/get?access_token=$ACCESS_TOKEN";
        $result = $this->curlOrder($url,$param); 
        return $result;
	}
    /**
     * [updateOrderStatus 修改订单状态]
     * @param  array   $assemblyInfo    [订单详情]
     * @param  integer $status          [订单状态，1:支付成功,2:支付失败,3:用户取消,4:超时未支付;5:商家取消;10:其他原因取消]
     * @param  integer $transaction_id  [支付订单号]
     * @param  string  $action_remark   [其他具体原因]
     */
    public function updateOrderStatus($assemblyInfo=[],$status = 1,$transaction_id = 0,$action_remark = '')
    {
        $data = [];
        $data['order_id'] = $assemblyInfo['order_id'];
        $data['out_order_id'] = $assemblyInfo['out_order_id'];
        $data['openid'] = $assemblyInfo['openid'];
        $data['action_type'] = $status;
        $data['action_remark'] = $action_remark;
        $data['transaction_id'] = $transaction_id;
        $data['pay_time'] = date('Y-m-d H:i:s',time());
        $res = $this->pay($data);
        if ($res['errcode'] > 0) {
            return $res['errmsg'];
        }
        return true;
    }
    /**
     * [getCompanyList 获取快递公司列表]
     */
    public function getCompanyList($is_again = false)
    {
        $company_list = Cache::get('company_list');
        if (empty($company_list) || $is_again == true) {
            $ACCESS_TOKEN = (new WeiXinMpModel)->getAccessToken();
            $url = "https://api.weixin.qq.com/shop/delivery/get_company_list?access_token=$ACCESS_TOKEN";
            $res = $this->curlOrder($url,$param);
            if ($res['errcode'] > 0) {
                return $res['errmsg'];
            }
            foreach ($res['company_list'] as $value) {
                $company_list[$value['delivery_id']] = $value['delivery_name'];
            }
            Cache::set('company_list',$company_list,86400);
        }
        return $company_list;
    }

	/**
	 * [addOrder 提交订单]
	 * @param array $param [请求参数]
	 */
    public function addOrder($param)
    {
        $ACCESS_TOKEN = (new WeiXinMpModel)->getAccessToken();
        $url = "https://api.weixin.qq.com/shop/order/add?access_token=$ACCESS_TOKEN";
        return $this->curlOrder($url,$param);
    }

    /**
     * [sandOrder 发货]
     * @param array $param [请求参数]
     */
    public function sandOrder($param)
    {
        $ACCESS_TOKEN = (new WeiXinMpModel)->getAccessToken();
        $url = "https://api.weixin.qq.com/shop/delivery/send?access_token=$ACCESS_TOKEN";
        return $this->curlOrder($url,$param);
    }
    /**
     * [signOrder description]
     * @param  array  $param [签收]
     */
    public function signOrder($param)
    {
        $ACCESS_TOKEN = (new WeiXinMpModel)->getAccessToken();
        $url = "https://api.weixin.qq.com/shop/delivery/recieve?access_token=$ACCESS_TOKEN";
        return $this->curlOrder($url,$param);
    }

    /**
     * [pay 同步支付结果]
     * @param array $param [请求参数]
     */
    public function pay($param)
    {
        $ACCESS_TOKEN = (new WeiXinMpModel)->getAccessToken();
        $url = "https://api.weixin.qq.com/shop/order/pay?access_token=$ACCESS_TOKEN";
        return $this->curlOrder($url,$param);
    }

	/**
     * [curlGoods curl]
     * @param  string $url   [description]
     * @param  [type] $param [description]
     * @return [type]        [description]
     */
    public function curlOrder($url='',$param = array())
    {
    	$data = json_encode($param,JSON_UNESCAPED_UNICODE);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $result = curl_exec($ch);
        if (curl_errno($ch)) {
            return curl_error($ch);
        }
        curl_close($ch);
        return json_decode($result,true);
    }
}